package com.ls.controller;

import com.alibaba.fastjson.JSON;
import com.ls.pojo.StorageRecord;
import com.ls.pojo.Supplier;
import com.ls.pojo.SysUser;
import com.ls.service.storageRecord.StorageRecordService;
import com.ls.service.supplier.SupplierService;
import com.ls.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/sys/storageRecord")
public class StorageRecordController {
    @Resource
    private StorageRecordService storageRecordService;
    @Resource
    private SupplierService supplierService;

    @RequestMapping({"/list"})
    public String list(Model model, @RequestParam(value = "queryGoodsName",required = false) String queryGoodsName, @RequestParam(value = "querySupplierId",required = false) Integer querySupplierId, @RequestParam(value = "queryPayStatus",required = false) Integer queryPayStatus, @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex) {
        List<Supplier> supplierList = null;
        List storageRecordList = null;

        try {
            Integer pageSize = 5;
            int totalCount = this.storageRecordService.getCount(queryGoodsName, querySupplierId, queryPayStatus);
            PageSupport pages = new PageSupport();
            pages.setCurrentPageNo(pageIndex);
            pages.setPageSize(pageSize);
            pages.setTotalCount(totalCount);
            int totalPageCount = pages.getTotalPageCount();
            if (pageIndex > totalPageCount) {
                pageIndex = totalPageCount;
            }

            try {
                storageRecordList = this.storageRecordService.getList(queryGoodsName, querySupplierId, queryPayStatus, pageIndex, pageSize);
                supplierList = this.supplierService.getList();
            } catch (Exception var13) {
                var13.printStackTrace();
            }

            model.addAttribute("storageRecordList", storageRecordList);
            model.addAttribute("supplierList", supplierList);
            model.addAttribute("queryGoodsName", queryGoodsName);
            model.addAttribute("querySupplierId", querySupplierId);
            model.addAttribute("queryPayStatus", queryPayStatus);
            model.addAttribute("totalPageCount", totalPageCount);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("currentPageNo", pageIndex);
            return "storageRecord/list";
        } catch (Exception var14) {
            var14.printStackTrace();
            return "redirect:/toSysError";
        }
    }

    @GetMapping(value = "/toAdd")
    public String toAdd(@ModelAttribute StorageRecord storageRecord) {
        return "storageRecord/add";
    }

    @GetMapping(value = "/toUpdate/{id}")
    public String toUpdate(@PathVariable Integer id, HttpServletRequest request, Model model) {
        StorageRecord storageRecord = null;

        try {
            storageRecord = storageRecordService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute(storageRecord);

        return "storageRecord/update";
    }

    @RequestMapping(value = "/update")
    public String update(StorageRecord storageRecord, HttpSession session) throws Exception {
        storageRecord.setUpdatedUserId(((SysUser)session.getAttribute("userSession")).getId());

        return storageRecordService.update(storageRecord) ? "redirect:/sys/storageRecord/list" : "storageRecord/update";
    }

    @ResponseBody
    @DeleteMapping(value = "/del/{id}")
    public String del(@PathVariable Integer id) {
        HashMap resultMap = new HashMap();

        try {
            if (storageRecordService.deleteById(id)) {
                resultMap.put("delResult", "true");
            } else {
                resultMap.put("delResult", "false");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return JSON.toJSONString(resultMap);
    }

    @GetMapping(value = "/view/{id}")
    public String view(@PathVariable Integer id, Model model) {
        StorageRecord storageRecord = null;

        try {
            storageRecord = storageRecordService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute(storageRecord);

        return "storageRecord/view";
    }
}